package com.tensquare.friend.service;

import com.tensquare.friend.dao.FriendDao;
import com.tensquare.friend.dao.NoFriendDao;
import com.tensquare.friend.pojo.Friend;
import com.tensquare.friend.pojo.NoFriend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by WF on 2020/4/4 15:42
 */
@Service
@Transactional
public class FriendService {
    @Autowired
    private FriendDao friendDao;
    @Autowired
    private NoFriendDao noFriendDao;
    //1.添加好友
    public int addFriend(String id, String friendid) {
        System.out.println("id = " + id);
        System.out.println("friendid = " + friendid);
        //1.1)根据userid及friendId查询是否添加过此用户
        int count = friendDao.selectCount(id,friendid);
        //1.1.1）如果存在此记录，就返回
        if(count > 0) return 0;
        //1.2)添加好友
        Friend friend = new Friend(id,friendid);
        friend.setIslike("0");      //第一次单方添加，此值为0
        friendDao.save(friend);     //添加到数据库中
        //1.3)反向查询对方是否添加过
        int n = friendDao.selectCount(friendid, id);
        if(n > 0){  //修改islike的值为1
            friendDao.updateIsLike("1",id,friendid);
            friendDao.updateIsLike("1",friendid,id);
        }
        return 1;
    }
    //2.删除用户
    public void delete(String id, String friendid) {
        //2.1)删除好友
        friendDao.deleteFriend(id,friendid);
        //2.2)更改islike的值为0
        friendDao.updateIsLike("0",id,friendid);
        //2.3)在非好友表中添加一条记录
        NoFriend noFriend = new NoFriend(id,friendid);
        noFriendDao.save(noFriend);
    }
}
